package com.shqcjd.preinstallmobile.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.preinstallmobile.R;
import com.shqcjd.preinstallmobile.api.ApiService;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.preinstallmobile.model.ResponseData;
import com.shqcjd.preinstallmobile.model.pojo.FileBean;
import com.shqcjd.preinstallmobile.ui.adapter.RemoteFileListAdapter;
import com.shqcjd.preinstallmobile.ui.base.BaseActivity;
import com.shqcjd.preinstallmobile.ui.base.BasePresenter;
import com.shqcjd.preinstallmobile.util.UIUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 远程指导记录的文件列表
 */
public class HistoryGuideFileActivity extends BaseActivity {


    @BindView(R.id.iv_toolbar_back)
    ImageView ivToolbarBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.rv_file_list)
    RecyclerView rvFileList;
    private int guideID;
    private RemoteFileListAdapter fileListAdapter;
    private View notDataView;
    private Context mContext;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_history_guide_file;
    }

    @Override
    public void initView() {
        super.initView();
        mContext = this;
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvFileList.getParent(), false);
        guideID = getIntent().getIntExtra("GuideID", -1);
        initAdapter();
    }

    private void initAdapter() {
        fileListAdapter = new RemoteFileListAdapter();
        rvFileList.setLayoutManager(new LinearLayoutManager(this));
        rvFileList.setAdapter(fileListAdapter);
        fileListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FileBean item = (FileBean) adapter.getItem(position);
                Intent intent = new Intent(mContext, UIUtils.previewClass(item.extension));
                intent.putExtra("name", item.filename);
                intent.putExtra("url", item.url);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        getData();
    }

    private void getData() {
        ApiService.getGuideFileList(guideID)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<List<FileBean>>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        showWaitingDialog("获取数据中");
                    }

                    @Override
                    public void _onNext(ResponseData<List<FileBean>> retData) {
                        if (retData.retCode== 10000) {
                            List<FileBean> data = retData.data;
                            if (data.size() > 0) {
                                fileListAdapter.setNewData(data);
                            } else {
                                fileListAdapter.setNewData(null);
                                fileListAdapter.setEmptyView(notDataView);
                            }
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        hideWaitingDialog();
                    }
                });
    }


    @OnClick(R.id.iv_toolbar_back)
    public void onViewClicked() {
        finish();
    }
}
